package tag

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"go-api-template/api/http"
	"go-api-template/api/models/system"
	"go-api-template/service"
)

type CreateReq struct {
	Name string `json:"name" binding:"required"`
}

func (req *CreateReq) Request(ctx *gin.Context, s *service.Service) (http.Resp, error) {
	return req.Do(ctx, s)
}
func (req *CreateReq) Do(ctx *gin.Context, s *service.Service) (resp http.Resp, err error) {
	var data system.Tag

	_ = copier.Copy(&data, req)

	s.Db.Create(&data)

	resp.SetData(data)
	return
}

type ListReq struct {
}
type ListResp struct {
	http.RespList[system.Tag]
}

func (req *ListReq) Request(ctx *gin.Context, s *service.Service) (http.Resp, error) {
	return req.Do(ctx, s)
}
func (req *ListReq) Do(ctx *gin.Context, s *service.Service) (resp http.Resp, err error) {
	var data ListResp

	s.Db.Find(&data.Items)

	resp.SetData(data)
	return
}

type DeleteReq struct {
	ID string `json:"id" binding:"required"`
}
type DeleteResp struct {
}

func (req *DeleteReq) Request(ctx *gin.Context, s *service.Service) (http.Resp, error) {
	return req.Do(ctx, s)
}
func (req *DeleteReq) Do(ctx *gin.Context, s *service.Service) (resp http.Resp, err error) {
	var data DeleteResp

	err = s.Db.Unscoped().Delete(&system.Tag{}, "id = ?", req.ID).Error

	resp.SetData(data)
	return
}
